#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Author : fanzf
# @Version：V 0.1
# @File : init_db.py.py
# @desc :

import sqlite3
from pathlib import Path

DATABASE = 'dbinstance/par.db'


def init_db():
    # 确保 instance 文件夹存在
    Path('dbinstance').mkdir(exist_ok=True)

    conn = sqlite3.connect(DATABASE)
    cursor = conn.cursor()

    # 创建 users 表
    cursor.execute('''
            CREATE TABLE IF NOT EXISTS user_login_info (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                username TEXT NOT NULL,
                password TEXT NOT NULL,
                cookie_info  TEXT NOT NULL,
                session_info TEXT NOT NULL,
                updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP

            )
        ''')

    # 创建 task 表
    cursor.execute('''
                CREATE TABLE IF NOT EXISTS task_info (
                    taskNo TEXT PRIMARY KEY,
                    taskType TEXT NOT NULL,
                    stagePlayId TEXT NOT NULL,
                    eventId TEXT NOT NULL,
                    ticketId TEXT NOT NULL,
                    editStock  TEXT NOT NULL,
                    `timestamp` TIMESTAMP NOT NULL,
                    channel TEXT NOT NULL,
                    taskStatus INTEGER,
                    failReason TEXT,
                    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP

                )
            ''')

    conn.commit()
    conn.close()


if __name__ == '__main__':
    init_db()
    print("Database initialized!")
